/*
 *  Copyright 2019-2020 Zheng Jie
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *  http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */
package me.zhengjie.tools.domain

import cn.hutool.core.bean.BeanUtil
import cn.hutool.core.bean.copier.CopyOptions
import io.swagger.v3.oas.annotations.media.Schema
import jakarta.persistence.Column
import jakarta.persistence.Entity
import jakarta.persistence.Id
import jakarta.persistence.Table
import jakarta.validation.constraints.NotBlank
import me.zhengjie.common.utils.JacksonUtil


/**
 * 七牛云对象存储配置类
 * @author Zheng Jie
 * @date 2018-12-31
 */
@Entity
@Table(name = "tool_qiniu_config")
data class QiniuConfig (
    @Id
    @Column(name = "config_id")
    @Schema(description  = "ID")
    var id: Long? = null,

    @NotBlank
    @Schema(description  = "accessKey")
    var accessKey: String? = null,

    @NotBlank
    @Schema(description  = "secretKey")
    var secretKey: String? = null,

    @NotBlank
    @Schema(description  = "存储空间名称作为唯一的 Bucket 识别符")
    var bucket: String? = null,

    /**
     * Zone表示与机房的对应关系
     * 华东	Zone.zone0()
     * 华北	Zone.zone1()
     * 华南	Zone.zone2()
     * 北美	Zone.zoneNa0()
     * 东南亚	Zone.zoneAs0()
     */
    @NotBlank
    @Schema(description  = "Zone表示与机房的对应关系")
    var zone: String? = null,

    @NotBlank
    @Schema(description  = "外链域名，可自定义，需在七牛云绑定")
    var host: String? = null,

    @Schema(description  = "空间类型：公开/私有")
    var type: String = "公开"
){
    annotation class Update

    companion object {
        fun fromJson(json: String): QiniuConfig? {
            val obj= JacksonUtil.parseObject(json, QiniuConfig::class.java)!!
            return when(obj){
                is QiniuConfig -> obj
                else -> null
            }
        }
    }
    fun tojson():String? {
        return JacksonUtil.toJSONString(this)
    }


    fun copy(source: QiniuConfig?) {
        BeanUtil.copyProperties(source, this, CopyOptions.create().setIgnoreNullValue(true))
    }
}